package cn.edu.qzuie.artist.module.td.controller.admin.submitactivity;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.submitactivity.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.submitactivity.SubmitActivityDO;
import cn.edu.qzuie.artist.module.td.service.submitactivity.SubmitActivityService;

@Tag(name = "管理后台 - 投稿")
@RestController
@RequestMapping("/td/submit-activity")
@Validated
public class SubmitActivityController {

    @Resource
    private SubmitActivityService submitActivityService;

    @PostMapping("/create")
    @Operation(summary = "创建投稿")
    @PreAuthorize("@ss.hasPermission('td:submit-activity:create')")
    public CommonResult<Long> createSubmitActivity(@Valid @RequestBody SubmitActivitySaveReqVO createReqVO) {
        return success(submitActivityService.createSubmitActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投稿")
    @PreAuthorize("@ss.hasPermission('td:submit-activity:update')")
    public CommonResult<Boolean> updateSubmitActivity(@Valid @RequestBody SubmitActivitySaveReqVO updateReqVO) {
        submitActivityService.updateSubmitActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投稿")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:submit-activity:delete')")
    public CommonResult<Boolean> deleteSubmitActivity(@RequestParam("id") Long id) {
        submitActivityService.deleteSubmitActivity(id);
        return success(true);
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得投稿")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('td:submit-activity:query')")
//    public CommonResult<SubmitActivityRespVO> getSubmitActivity(@RequestParam("id") Long id) {
//        SubmitActivityDO submitActivity = submitActivityService.getSubmitActivity(id);
//        return success(BeanUtils.toBean(submitActivity, SubmitActivityRespVO.class));
//    }
    @GetMapping("/get")
    @Operation(summary = "获得投稿")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:submit-activity:query')")
    public CommonResult<SubmitActivitysRespVO> getSubmitActivity(@RequestParam("id") Long id) {
        SubmitActivitysRespVO submitActivity = submitActivityService.getSubmitActivityWithName(id);
        return success(submitActivity);
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得投稿分页")
//    @PreAuthorize("@ss.hasPermission('td:submit-activity:query')")
//    public CommonResult<PageResult<SubmitActivityRespVO>> getSubmitActivityPage(@Valid SubmitActivityPageReqVO pageReqVO) {
//        PageResult<SubmitActivityDO> pageResult = submitActivityService.getSubmitActivityPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, SubmitActivityRespVO.class));
//    }
    @GetMapping("/page")
    @Operation(summary = "获得投稿分页")
    @PreAuthorize("@ss.hasPermission('td:submit-activity:query')")
    public CommonResult<PageResult<SubmitActivityPagesRespVO>> getSubmitActivityPage(@Valid SubmitActivityPageReqVO pageReqVO) {
        PageResult<SubmitActivityPagesRespVO> pageResult = submitActivityService.getSubmitActivityPageWithName(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SubmitActivityPagesRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出投稿 Excel")
    @PreAuthorize("@ss.hasPermission('td:submit-activity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSubmitActivityExcel(@Valid SubmitActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubmitActivityDO> list = submitActivityService.getSubmitActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投稿.xls", "数据", SubmitActivityRespVO.class,
                        BeanUtils.toBean(list, SubmitActivityRespVO.class));
    }

}